package com.livepan.business.controller;

import com.livepan.server.BaseController;
import com.livepan.server.dao.CourseCategoryDOMapper;
import com.livepan.server.error.BusinessException;
import com.livepan.server.response.CommonReturnType;
import com.livepan.server.service.CourseCategoryService;
import com.livepan.server.service.CourseContentService;
import com.livepan.server.service.CourseService;
import com.livepan.server.service.model.CourseContentModel;
import com.livepan.server.service.model.CourseModel;
import com.livepan.server.service.model.PageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {

    public static final String BUSINESS_NAME = "课程";

    private static final Logger LOG = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseCategoryService courseCategoryService;

    @Autowired
    private CourseContentService courseContentService;

    @ResponseBody
    @RequestMapping("/list")
    public CommonReturnType course(PageModel pageModel) {
        LOG.info("page:" + pageModel.getPage());
        courseService.list(pageModel);
        return CommonReturnType.create(pageModel);
    }

    @ResponseBody
    @RequestMapping("/save")
    public CommonReturnType saveCourse(@RequestBody CourseModel courseModel) throws BusinessException {
        courseService.save(courseModel);
        return CommonReturnType.create(courseModel);
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public CommonReturnType delete(@PathVariable String id) {
        courseService.delete(id);
        return CommonReturnType.create(null);
    }

    @ResponseBody
    @RequestMapping("/listCategory")
    public CommonReturnType listCategory(String courseId) {
        return CommonReturnType.create(courseCategoryService.listCourseCategory(courseId));
    }

    @ResponseBody
    @RequestMapping("/saveCourseContent")
    public CommonReturnType saveCourseContent(@RequestBody CourseContentModel courseContentModel) throws BusinessException {
        courseContentService.save(courseContentModel);
        return CommonReturnType.create(courseContentModel);
    }

    @ResponseBody
    @RequestMapping("/getCourseContent")
    public CommonReturnType getCourseContent(String courseId) throws BusinessException {
        return CommonReturnType.create(courseContentService.getCourseContent(courseId));
    }
}
